package com.growthserver.controller;


import com.github.pagehelper.PageInfo;
import com.growthserver.entity.IntegralBudgetEntity;
import com.growthserver.enums.ResultBeans;
import com.growthserver.entity.TTaskAssignEntity;
import com.growthserver.entity.UserInfoEntity;
import com.growthserver.param.GrowthParam;
import com.growthserver.param.IntegralBudgetParam;
import com.growthserver.service.GrowthCenterService;
import com.growthserver.vo.IntegralBudgetVo;
import com.growthserver.vo.TTaskAssignVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "/mp/portal/growth/growthCenter", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
@Api(description = "成长中心")
public class GrowthCenterController {

    @Autowired
    private GrowthCenterService growthCenterService;

    /**
     * 发放积分成长值列表展示
     */
    @ApiOperation(value = "发放积分成长值列表展示", notes = "列表展示,根据type区分展示内容")
    @PostMapping("/detailQuery")
//    @ApisInfo(apis = {
//            @GwApiDesc(name = "ly.mp.portal.growth.center.detailQuery", type = ApiType.PORTAL)
//    }, remark = "成长中心--发放积分成长值列表展示", qps = 100, connectTimeout = 5)
    public ResultBeans<List<TTaskAssignEntity>> memberListDisplay(@RequestBody GrowthParam growthParam) {
        ResultBeans<List<TTaskAssignEntity>> result = new ResultBeans<>();
        try {
            List<TTaskAssignEntity> tIovIntegralDetails = growthCenterService.detailQuery(growthParam);
            PageInfo<TTaskAssignEntity> tPageInfo = new PageInfo<>(tIovIntegralDetails);
            result.setResult("1");
            result.setRows(tIovIntegralDetails);
            result.setTotal((int) tPageInfo.getTotal());
            result.setMsg("查询成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("展示异常");
            result.setRows(new ArrayList<>());
            ;
        }
        return result;
    }

    /**
     * 积分批量发放
     */
    @ApiOperation(value = "积分批量发放", notes = "列表展示,根据type区分展示内容")
    @PostMapping(value = "/batchSend")
//    @ApisInfo(apis = {
//            @GwApiDesc(name = "ly.mp.portal.growth.center.batchSend", type = ApiType.PORTAL)
//    }, remark = "成长中心--积分批量发放", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> batchSend(@RequestHeader(required = false, name = "uid") String uid,
                                         @RequestHeader(required = false, name = "userName") String userName,
                                         @RequestParam("taskName" ) String taskName,
                                         @RequestParam("taskType" ) String taskType,
                                         @RequestParam("taskDistributeDepartment" ) String taskDistributeDepartment,
                                         @RequestParam("doType" ) String doType,
                                         @RequestParam("integralValue" ) String integralValue,
                                         @RequestParam("growthValue" ) String growthValue,
                                         @RequestParam("doReason" ) String doReason,
                                         @RequestParam("operateType" ) String operateType,
                                         @RequestParam("taskValueType" ) String taskValueType,
                                         @RequestParam("requestNum" ) String requestNum,
                                         @RequestParam("file") MultipartFile file) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            //组装参数
            TTaskAssignVo tTaskAssignVo = new TTaskAssignVo();
                //任务名称
            tTaskAssignVo.setTaskName(taskName);
                //任务类型
            tTaskAssignVo.setTaskType(taskType);
                //发放部门
            tTaskAssignVo.setTaskDistributeDepartment(taskDistributeDepartment);
                //积分预算申请编号
            tTaskAssignVo.setRequestNum(requestNum);
                //发放/扣减
            tTaskAssignVo.setDoType(doType);
                //积分数值
            tTaskAssignVo.setIntegralValue(integralValue);
                //成长值数值
            tTaskAssignVo.setGrowthValue(growthValue);
                //补发原因
            tTaskAssignVo.setDoReason(doReason);
                //分值类型:1积分值 2成长值 3积分+成长值
            tTaskAssignVo.setTaskValueType(taskValueType);
                //操作类型(1批量,2单独)
            tTaskAssignVo.setOperateType(operateType);
            tTaskAssignVo.setCreator(uid);
            tTaskAssignVo.setCreatedName(userName);
            //进行逻辑处理
            result = growthCenterService.batchSingleSendSendSend(tTaskAssignVo, file);
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("积分批量发放异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }


    /**
     * 积分单独发放
     */
    @ApiOperation(value = "积分单独发放", notes = "列表展示,根据type区分展示内容")
    @PostMapping(value = "/singleSend")
//    @ApisInfo(apis = {
//            @GwApiDesc(name = "ly.mp.portal.growth.center.singleSend", type = ApiType.PORTAL),
//            @GwApiDesc(name = "ly.mp.inner.growth.center.singleSend", type = ApiType.INNER_SERVER)
//    }, remark = "成长中心--积分单独发放", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> singleSendSend(@RequestHeader(required = false, name = "uid") String uid,
                                         @RequestHeader(required = false, name = "userName") String userName,
                                         @RequestBody TTaskAssignVo tTaskAssignVo) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            tTaskAssignVo.setCreator(uid);
            tTaskAssignVo.setCreatedName(userName);
            //进行逻辑处理
            result = growthCenterService.batchSingleSendSendSend(tTaskAssignVo, null);
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("积分单独发放异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 查看单个发布任务详情,获取用户ID返回即可
     */
    @ApiOperation(value = "查看单个发布任务详情", notes = "列表展示,根据type区分展示内容")
    @PostMapping(value = "/queryMemberIds")
//    @ApisInfo(apis = {
//            @GwApiDesc(name = "ly.mp.portal.growth.center.queryMemberIds", type = ApiType.PORTAL)
//    }, remark = "成长中心--查看单个发布任务详情", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> queryMemberIds(@RequestBody GrowthParam growthParam) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            List<HashMap<String,String>> list = growthCenterService.queryMemberIds(growthParam);
            result.setRows(list);
            result.setResult("1");
            result.setMsg("查看单个发布任务详情成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("查看单个发布任务详情异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 删除单个发放任务
     */
    @ApiOperation(value = "删除单个发放任务", notes = "列表展示,根据type区分展示内容")
    @PostMapping(value = "/deleteById")
//    @ApisInfo(apis = {
//            @GwApiDesc(name = "ly.mp.portal.growth.center.deleteById", type = ApiType.PORTAL)
//    }, remark = "成长中心--删除单个发放任务", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> deleteById(@RequestBody GrowthParam growthParam) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
             growthCenterService.deleteById(growthParam);
            result.setRows(new ArrayList<>());
            result.setResult("1");
            result.setMsg("删除单个发放任务成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("删除单个发放任务异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 用户积分明细表
     */
    @ApiOperation(value = "用户积分明细表", notes = "列表展示,根据type区分展示内容")
    @PostMapping(value = "/userInfo")
//    @ApisInfo(apis = {
//            @GwApiDesc(name = "ly.mp.portal.growth.center.userInfo", type = ApiType.PORTAL)
//    }, remark = "成长中心--用户积分明细表", qps = 100, connectTimeout = 5)
    public ResultBeans<List<UserInfoEntity>> userInfo(@RequestBody GrowthParam growthParam) {
        ResultBeans<List<UserInfoEntity>> result = new ResultBeans<>();
        try {
            List<UserInfoEntity> userInfo = growthCenterService.getUserInfo(growthParam);
            result.setRows(userInfo);
            result.setResult("1");
            result.setMsg("用户积分明细表查询成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("用户积分明细表查询异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 任务类型枚举
     */
    @ApiOperation(value = "任务类型枚举", notes = "列表展示,根据type区分展示内容")
    @PostMapping(value = "/taskTypeEnum")
//    @ApisInfo(apis = {
//            @GwApiDesc(name = "ly.mp.portal.growth.center.taskTypeEnum", type = ApiType.PORTAL)
//    }, remark = "成长中心--任务类型枚举", qps = 100, connectTimeout = 5)
    public ResultBeans<List<HashMap<String,String>>> taskTypeEnum(@RequestBody GrowthParam growthParam) {
        ResultBeans<List<HashMap<String,String>>> result = new ResultBeans<>();
        try {
            List<HashMap<String,String>> list = growthCenterService.taskTypeEnum(growthParam);
            result.setRows(list);
            result.setResult("1");
            result.setMsg("任务类型枚举查询成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("任务类型枚举查询异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 积分预算列表展示
     */
    @ApiOperation(value = "积分预算列表展示", notes = "列表展示,根据type区分展示内容")
    @PostMapping("/integralBudgetDisplay")
//    @ApisInfo(apis = {
//            @GwApiDesc(name = "ly.mp.portal.growth.integral.integralBudgetDisplay", type = ApiType.PORTAL)
//    }, remark = "积分预算--积分预算列表展示", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> integralBudgetDisplay(@RequestBody IntegralBudgetParam integralBudgetParams) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            List<IntegralBudgetEntity> tIovIntegralBudgets = growthCenterService.integralBudgetDisplay(integralBudgetParams);
            PageInfo<IntegralBudgetEntity> tPageInfo = new PageInfo<>(tIovIntegralBudgets);
            result.setResult("1");
            result.setRows(tIovIntegralBudgets);
            result.setTotal((int) tPageInfo.getTotal());
            result.setMsg("积分预算列表查询成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("积分预算列表查询异常");
            result.setRows(new ArrayList<>());
            ;
        }
        return result;
    }

    /**
     * 积分预算创建
     */
    @ApiOperation(value = "积分预算任务创建", notes = "列表展示,根据type区分展示内容")
    @PostMapping("/integralBudgetCreate")
//    @ApisInfo(apis = {
//            @GwApiDesc(name = "ly.mp.portal.growth.integral.integralBudgetCreate", type = ApiType.PORTAL)
//    }, remark = "积分预算--积分预算任务创建", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> integralBudgetCreate(@RequestBody IntegralBudgetVo integralBudgetVo,
                                                    @RequestHeader(required = false, name = "uid") String uid,
                                                    @RequestHeader(required = false, name = "userName") String userName) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            integralBudgetVo.setCreator(uid);
            integralBudgetVo.setCreatedName(userName);
            growthCenterService.integralBudgetCreate(integralBudgetVo);
            result.setResult("1");
            result.setRows(new ArrayList<>());
            result.setMsg("积分预算任务创建成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("积分预算任务创建异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 积分预算审核
     */
    @ApiOperation(value = "积分预算审核", notes = "列表展示,根据type区分展示内容")
    @PostMapping("/integralBudgetAudit")
//    @ApisInfo(apis = {
//            @GwApiDesc(name = "ly.mp.portal.growth.integral.integralBudgetAudit", type = ApiType.PORTAL)
//    }, remark = "积分预算--积分预算审核", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> integralBudgetAudit(@RequestBody IntegralBudgetEntity integralBudgetEntity,
                                                   @RequestHeader(required = false, name = "uid") String uid,
                                                   @RequestHeader(required = false, name = "userName") String userName) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            integralBudgetEntity.setModifier(uid);
            integralBudgetEntity.setModifyName(userName);
            growthCenterService.integralBudgetAudit(integralBudgetEntity);
            result.setResult("1");
            result.setRows(new ArrayList<>());
            result.setMsg("积分预算审核成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("积分预算审核异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 积分预算Excel导入
     */
    @ApiOperation(value = "积分预算Excel导入", notes = "列表展示,根据type区分展示内容")
    @PostMapping("/integralBudgetExcel")
//    @ApisInfo(apis = {
//            @GwApiDesc(name = "ly.mp.portal.growth.integral.integralBudgetExcel", type = ApiType.PORTAL)
//    }, remark = "积分预算--积分预算Excel导入", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> integralBudgetExcel(@RequestHeader(required = false, name = "uid") String uid,
                                                   @RequestHeader(required = false, name = "userName") String userName,
                                                   @RequestParam("file") MultipartFile file) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
           return growthCenterService.integralBudgetExcel(uid,userName,file);
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("积分预算Excel导入异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

}
